package com.wcpdoc.survey.survey.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wcpdoc.survey.core.dao.BaseDao;
import com.wcpdoc.survey.core.service.impl.BaseServiceImp;
import com.wcpdoc.survey.core.util.ValidateUtil;
import com.wcpdoc.survey.survey.dao.SurveyTypeDao;
import com.wcpdoc.survey.survey.entity.SurveyType;
import com.wcpdoc.survey.survey.service.SurveyTypeExService;
import com.wcpdoc.survey.survey.service.SurveyTypeService;
/**
 * 调查分类服务层实现
 * 
 * v1.0 zhanghc 2017-06-28 21:34:41
 */
@Service
public class SurveyTypeServiceImpl extends BaseServiceImp<SurveyType> implements SurveyTypeService{
	@Resource
	private SurveyTypeDao surveyTypeDao;
	@Resource
	private SurveyTypeExService surveyTypeExService;

	@Override
	@Resource(name = "surveyTypeDaoImpl")
	public void setDao(BaseDao<SurveyType> dao) {
		super.dao = dao;
	}

	@Override
	public void saveAndUpdate(SurveyType surveyType) {
		//校验数据有效性
		if(!ValidateUtil.isValid(surveyType.getName())){
			throw new RuntimeException("无法获取参数：surveyType.name");
		}
		if(existName(surveyType)){
			throw new RuntimeException("名称已存在！");
		}
				
		//保存调查分类
		if(surveyType.getParentId() == null){
			surveyType.setParentId(0);
		}
		surveyTypeDao.save(surveyType);
		
		//更新父子关系
		SurveyType parentSurveyType = surveyTypeDao.getEntity(surveyType.getParentId());
		if(parentSurveyType == null){
			surveyType.setParentSub("_" + surveyType.getId() + "_");
		}else {
			surveyType.setParentSub(parentSurveyType.getParentSub() + surveyType.getId() + "_");
		}
		surveyTypeDao.update(surveyType);
	}
	
	@Override
	public void editAndUpdate(SurveyType surveyType) {
		//校验数据有效性
		if(!ValidateUtil.isValid(surveyType.getName())){
			throw new RuntimeException("无法获取参数：surveyType.name");
		}
		if(existName(surveyType)){
			throw new RuntimeException("名称已存在！");
		}
		
		//修改调查分类
		surveyTypeDao.update(surveyType);
	}

	@Override
	public void delAndUpdate(Integer[] ids) {
		//校验数据有效性
		if(!ValidateUtil.isValid(ids)){
			throw new RuntimeException("无法获取参数：ids");
		}
		
		//删除调查分类，不包括根调查分类
		for(Integer id : ids){
			if(id == 1){
				continue;
			}
			
			List<SurveyType> surveyTypeList = surveyTypeDao.getAllSubSurveyTypeList(id);
			for(SurveyType surveyType : surveyTypeList){
				if(surveyType.getState().equals("0")){
					continue;
				}
				
				surveyType.setState(0);
				surveyTypeDao.update(surveyType);
				
				surveyTypeExService.delAndUpdate(surveyType);
			}
		}
	}

	@Override
	public List<Map<String, Object>> getTreeList() {
		return surveyTypeDao.getTreeList();
	}

	@Override
	public void doMove(Integer sourceId, Integer targetId) {
		//校验数据有效性
		if(sourceId == null){
			throw new RuntimeException("无法获取参数：sourceId");
		}
		if(targetId == null){
			throw new RuntimeException("无法获取参数：targetId");
		}
		
		//移动调查分类
		surveyTypeDao.doMove(sourceId, targetId);
	}

	/**
	 * 名称是否已存在
	 * v1.0 zhanghc 2017-06-28 21:34:41
	 * @param surveyType
	 * @return boolean
	 */
	private boolean existName(SurveyType surveyType){
		//校验数据有效性
		if(surveyType == null){
			throw new RuntimeException("无法获取参数：surveyType");
		}
		if(!ValidateUtil.isValid(surveyType.getName())){
			throw new RuntimeException("无法获取参数：surveyType.name");
		}
		
		//如果是添加
		SurveyType surveyType2 = surveyTypeDao.getSurveyTypeByName(surveyType.getName());
		if(surveyType2 != null){
			surveyTypeDao.evict(surveyType2);
		}
		
		if(surveyType.getId() == null){
			if(surveyType2 != null){
				return true;
			}
			return false;
		}
		
		//如果是修改
		if(surveyType2 != null && !surveyType.getId().equals(surveyType2.getId())){
			return true;
		}
		return false;
	}
}
